package com.wxzz.elearing.course.api.app;

import com.alibaba.fastjson.JSONObject;
import com.wxzz.elearing.course.dto.*;
import com.wxzz.elearing.course.entity.*;
import com.wxzz.elearing.course.service.ICurriculumSectionService;
import com.wxzz.elearing.course.service.ICurriculumService;
import com.wxzz.elearing.course.service.IWxUserCurriculumService;
import com.wxzz.elearing.course.vo.StudyTimeVo;
import com.wxzz.elearing.course.vo.WxUserCenterVo;
import com.wxzz.elearning.common.dto.FeignWxUserDto;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/wx-curriculum")
@Api(tags = "课程接口")
public class WxCurriculumController {

    @Autowired
    private ICurriculumService curriculumService;

    @Autowired
    private IWxUserCurriculumService wxUserCurriculumService;

    @Autowired
    private ICurriculumSectionService curriculumSectionService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @ApiOperation(value = "课程中心", notes = "课程中心", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "listCurriculumByCategory", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<Curriculum>>> listCurriculumByCategory(HttpServletResponse response, @RequestHeader String token, @RequestBody CurriculumDto curriculumDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        //小程序端只展示上架
        curriculumDto.setStatus(2);
        //如果展示企业内部课程 则传企业id
        if (curriculumDto.getCurriculumType() != null) {
            if (curriculumDto.getCurriculumType() == 1 || curriculumDto.getCurriculumType() == 2 || curriculumDto.getCurriculumType() == 3) {
                curriculumDto.setEnterpriseId(jsonObject.getLong("enterpriseId"));
            }
        }

        return curriculumService.listCurriculumByCategory(response, 1, curriculumDto, 0);
    }

    @ApiOperation(value = "查询学习时长(学习报告)(小程序)", notes = "查询企业的学习时长(学习报告)(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "selectStudyTimeVoByEnterpriseId", method = RequestMethod.POST)
    public ReturnVO<StudyTimeVo> selectStudyTimeVoByEnterpriseId(@RequestHeader String token) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return curriculumService.selectStudyTimeVoByEnterpriseId(jsonObject.getLong("enterpriseId"));
    }

    @ApiOperation(value = "查询该企业的课程英雄榜(小程序)", notes = "查询该企业的课程英雄榜(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "selectFeignWxUserDtoByCurriculum", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<FeignWxUserDto>>> selectFeignWxUserDtoByCurriculum(HttpServletResponse response,@RequestHeader String token, @Valid @RequestBody FeignWxUserDtoByCurriculumDto feignWxUserDtoByCurriculumDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return curriculumService.selectFeignWxUserDtoByCurriculum(response,jsonObject.getLong("enterpriseId"), feignWxUserDtoByCurriculumDto);
    }

    @ApiOperation(value = "查询该企业明星讲师英雄榜(小程序)", notes = "查询该企业明星讲师英雄榜(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "selectFeignWxUserDtoByDocent", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<FeignWxUserDto>>> selectFeignWxUserDtoByDocent(HttpServletResponse response,@RequestHeader String token, @Valid @RequestBody FeignWxUserDtoByCurriculumDto feignWxUserDtoByCurriculumDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return curriculumService.selectFeignWxUserDtoByDocent(response,jsonObject.getLong("enterpriseId"), feignWxUserDtoByCurriculumDto);
    }

    @ApiOperation(value = "个人中心数据(小程序)", notes = "个人中心数据(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "selectWxUserCenterVo", method = RequestMethod.POST)
    public ReturnVO<WxUserCenterVo> selectWxUserCenterVo(@RequestHeader String token) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return curriculumService.selectWxUserCenterVo(jsonObject.getLong("id"));
    }

    @ApiOperation(value = "培训计划列表", notes = "培训计划列表", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "listCurriculumByCategory4train", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<Curriculum>>> listCurriclistCurriculumByDocentIdulumByCategory4train(@RequestHeader String token, @RequestBody CurriculumByCategory4trainDto curriculumByCategory4trainDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return curriculumService.listCurriculumByCategory4train(jsonObject.getLong("enterpriseId"), jsonObject.getLong("id"), curriculumByCategory4trainDto);
    }

    @ApiOperation(value = "查询课程详情", notes = "查询课程详情", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "selectCurriculumDetail/{curriculumId}", method = RequestMethod.POST)
    public ReturnVO<Curriculum> selectCurriculumDetail(@RequestHeader String token, @ApiParam("课程id") @PathVariable Long curriculumId) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return curriculumService.wxSelectCurriculumDetail(jsonObject.getLong("enterpriseId"), jsonObject.getLong("id"), curriculumId);
    }

    @ApiOperation(value = "查询课程章节列表", notes = "查询课程章节列表", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "listCurriculumSection/{curriculumId}", method = RequestMethod.POST)
    public ReturnVO<List<CurriculumUnit>> listCurriculumSection(@RequestHeader String token, @ApiParam("课程id") @PathVariable Long curriculumId) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return curriculumService.listCurriculumSection(jsonObject.getLong("id"), curriculumId);
    }

    @ApiOperation(value = "记录上次看的学习计划中的章节(点击播放时调用)", notes = "记录上次看的学习计划中的章节(点击播放时调用)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "setLastSee/{curriculumSectionId}", method = RequestMethod.POST)
    public ReturnVO setLastSee(@RequestHeader String token, @ApiParam("课程章节id") @PathVariable Long curriculumSectionId) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return curriculumSectionService.setLastSee(jsonObject.getLong("id"), curriculumSectionId);
    }

    @ApiOperation(value = "记录该用户对应课程的视频进度(退出时调用)", notes = "记录该用户对应课程的视频进度(退出时调用)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "setReturnSee/{curriculumId}/{seeTime}/{seeStatus}", method = RequestMethod.POST)
    public ReturnVO setReturnSee(@RequestHeader String token, @ApiParam("课程id") @PathVariable Long curriculumId,
                                 @ApiParam("学习时长(秒)") @PathVariable Long seeTime,
                                 @ApiParam("是否学完 0-正在学 1-已学完") @PathVariable Integer seeStatus) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return curriculumSectionService.setReturnSee(jsonObject.getLong("id"), curriculumId, seeTime, seeStatus);
    }

    @ApiOperation(value = "查询课程章节测评/查询在线考试测评", notes = "查询课程章节测评/查询在线考试测评", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "listCurriculumSectionTopic/{curriculumSectionId}/{topicCategory}", method = RequestMethod.POST)
    public ReturnVO<List<CurriculumSectionTopic>> listCurriculumSectionTopic(@ApiParam("课程章节id") @PathVariable Long curriculumSectionId,
                                                                             @ApiParam("0-章节测评 1-在线考试 2-培训调查题目") @PathVariable Integer topicCategory) {
        return curriculumService.listCurriculumSectionTopic(curriculumSectionId, topicCategory);
    }

    @ApiOperation(value = "根据讲师id列出课程", notes = "根据讲师id列出课程", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "listCurriculumByDocentId", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<Curriculum>>> listCurriculumByDocentId(@RequestBody CurriculumByDocentDto curriculumByDocentDto) {
        return curriculumService.listCurriculumByDocentId(curriculumByDocentDto);
    }

    @ApiOperation(value = "查询我的课程", notes = "查询我的课程", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "listWxUserCurriculumByWxUserId", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<Curriculum>>> listWxUserCurriculumByWxUserId(@RequestHeader String token, @RequestBody WxUserCurriculumDto wxUserCurriculumDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return wxUserCurriculumService.listWxUserCurriculumByWxUserId(jsonObject.getLong("id"), wxUserCurriculumDto);
    }

    @ApiOperation(value = "保存草稿箱", notes = "保存草稿箱", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "saveBox/{saveType}", method = RequestMethod.POST)
    public ReturnVO saveBox(@RequestHeader String token, @ApiParam("0-工厂体检 1-星级认证 2-能力测评 3-在线考试") @PathVariable Integer saveType, @RequestBody List<CurriculumSectionTopic> curriculumSectionTopics) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return wxUserCurriculumService.saveBox(jsonObject.getLong("id"), saveType, curriculumSectionTopics);
    }

    @ApiOperation(value = "用户购买课程", notes = "用户购买课程", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "saveWxUserCurriculum/{curriculumId}", method = RequestMethod.POST)
    public ReturnVO saveWxUserCurriculum(@RequestHeader String token, @PathVariable Long curriculumId) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return wxUserCurriculumService.saveWxUserCurriculum(jsonObject.getLong("id"), curriculumId);
    }

    @ApiOperation(value = "查看参与学习人", notes = "查看参与学习人", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "listWxUserCurriculum", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<WxUserCurriculum>>> listWxUserCurriculum(@RequestBody UserCurriculumDto userCurriculumDto) {
        return wxUserCurriculumService.listWxUserCurriculum(userCurriculumDto);
    }

    @ApiOperation(value = "用户收藏或取消课程", notes = "用户收藏或取消课程", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "collectCurriculum/{curriculumId}", method = RequestMethod.POST)
    public ReturnVO collectCurriculum(@RequestHeader String token, @PathVariable Long curriculumId) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return wxUserCurriculumService.collectCurriculum(jsonObject.getLong("id"), curriculumId);
    }
}
